package cn.shop.dys.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.activity.CxkbActivity;
import cn.shop.dys.activity.LimitBuyActivity;
import cn.shop.dys.activity.NewProductActivty2;
import cn.shop.dys.activity.ProductActivity;
import cn.shop.dys.activity.ProductListShowActivity;
import cn.shop.dys.activity.RecommmendBrandActivity;
import cn.shop.dys.bean.HomeRes;
import cn.shop.dys.bean.HomeTopicEntity;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.dys.constant.Constant;
import cn.shop.dys.util.ComUtil;
import cn.shop.lib_common.CommonUtil;
import cn.shop.lib_common.wiget.autorollLayout.AutoRollLayout;
import cn.shop.lib_common.wiget.autorollLayout.bean.IRollItem;


public class HomeFragment extends BaseFragment {

    private List<HomeTopicEntity> items = new ArrayList();
    @ViewInject(R.id.iv_time_top_1)
    private ImageView iv_rubing;
    @ViewInject(R.id.home_arl)
    private AutoRollLayout home_arl;
    @ViewInject(R.id.home_srf)
    private SwipeRefreshLayout srf;

    private String TAG = getClass().getSimpleName();
    private boolean hasInit;


    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!hasInit) {
            hasInit = true;
            initView();
            initDatas();
            initEvent(view);
        }
    }

    private void initView() {
        Constant.setViewHeightAndWidth(home_arl, Constant.SCREEN_HEIGHT / 3, null);
        Constant.setViewHeightAndWidth(iv_rubing, Constant.SCREEN_HEIGHT * 293 / 1080, Constant.SCREEN_WIDTH);
        srf.setOnRefreshListener(srfListener);
        home_arl.setOnItemClickListenter(onItemClickListener);
    }

    @Override
    public void onStart() {
        super.onStart();
        Glide.with(this).load(R.mipmap.top_bn).centerCrop().crossFade().into(iv_rubing);
    }

    @Override
    public void onResume() {
        super.onResume();
        home_arl.setAutoRoll(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        home_arl.setAutoRoll(false);
    }

    @Override
    public void onStop() {
        super.onStop();
        iv_rubing.setImageBitmap(null);
    }

    private void initDatas() {
        String homeUrl = CommonConstant.HOME_URL;

        mHttpUtils.send(HttpRequest.HttpMethod.GET, homeUrl, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                try {
                    HomeRes homeRes = mGson.fromJson(result, HomeRes.class);
                    List<HomeTopicEntity> homeTopic = homeRes.getHomeTopic();
                    items.clear();
                    items.addAll(homeTopic);

                    home_arl.setItems(items);
                    if (srf.isRefreshing()) {
                        srf.setRefreshing(false);
                    }
                } catch (Exception e) {
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                if (srf.isRefreshing()) {
                    srf.setRefreshing(false);
                }
                e.printStackTrace();
            }
        });
    }

    private SwipeRefreshLayout.OnRefreshListener srfListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            initDatas();
        }
    };

    //。。。。。。。。。。。。。。。。。。。。点击事件。。。。。。。。。。。。。。。。。。。。


    private void initEvent(final View view) {

        CommonUtil.runOnThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < ids.length; i++) {

                    view.findViewById(ids[i]).setOnClickListener(listener);
                }
            }
        });
    }

    private AutoRollLayout.OnItemClickListenter onItemClickListener = new AutoRollLayout.OnItemClickListenter() {
        @Override
        public void onItemClick(ViewGroup parent, int position, IRollItem item) {
            HomeTopicEntity entity = items.get(position);
            int id = entity.getId();
            switch (id) {
                case 130:
                    id = 1201;
                    break;
                case 131:
                    id = 1202;
                    break;
                case 133:
                    id = 1206;
                    break;

            }

            Intent intent = new Intent(mContext, ProductListShowActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("key", "brand/plist");
            bundle.putString("id", "" + id); //品牌id..
            intent.putExtras(bundle);
            startActivity(intent);
            return;
        }
    };
    //0-3
    int[] ids = new int[]{R.id.home_rl_news, R.id.home_rl_hot, R.id.home_rl_cuxiao, R.id.home_rl_time,
            //4-6 自然堂
            R.id.home_brand_zrt, R.id.home_chando_details1, R.id.home_chando_details2
            //7-10实时推荐
            , R.id.ll_time_top_1, R.id.iv_time_top_1, R.id.iv_time_top_2, R.id.iv_time_top_3,
            //11-13珀莱雅
            R.id.home_brand_bly, R.id.iv_haiyang1, R.id.iv_haiyang2
            //14-17热销品牌
            , R.id.home_iv_01, R.id.home_iv_02, R.id.home_iv_03, R.id.home_iv_04
    };
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (ids[0] == id) {
                //去新品上架
                Intent intent = new Intent(mContext, NewProductActivty2.class);
                startActivity(intent);
                return;
            }
            if (ids[1] == id) {
                //去热门单品
                ComUtil.startActivityOrService(getActivity(), RecommmendBrandActivity.class);
                return;
            }
            if (ids[2] == id) {
                //去促销快报
                startActivity(new Intent(mContext, CxkbActivity.class));
                return;
            }
            if (ids[3] == id) {
                //去限时抢购
                ComUtil.startActivityOrService(getActivity(), LimitBuyActivity.class);
                return;
            }

            if (ids[4] == id) {
                //去自然堂
                Intent intent = new Intent(mContext, ProductListShowActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("key", "brand/plist");
                bundle.putString("id", "" + 1207); //品牌id..
                intent.putExtras(bundle);
                startActivity(intent);
                return;
            }
            if (ids[5] == id) {
                //去自然堂凌时冰肌水
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 4);
                mContext.startActivity(intent);
                return;
            }
            if (ids[6] == id) {
                //去自然堂凌时洁面霜
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 5);
                mContext.startActivity(intent);
                return;
            }

            if (ids[7] == id) {
                //去清润天才冰心霜详情页
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 13);
                mContext.startActivity(intent);
                return;
            }
            if (ids[8] == id) {
                //去清润天才冰心霜购买页
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 16);
                mContext.startActivity(intent);
                return;
            }
            if (ids[9] == id) {
                //去墨菊深度补水霜详情页
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 23);
                mContext.startActivity(intent);
                return;
            }
            if (ids[10] == id) {
                //去兰蔻小黑瓶详情页
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 39);
                mContext.startActivity(intent);
                return;
            }


            if (ids[11] == id) {
                //去珀莱雅
                Toast.makeText(v.getContext(), "去珀莱雅", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, ProductListShowActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("key", "brand/plist");
                bundle.putString("id", "" + 1209); //品牌id..
                intent.putExtras(bundle);
                startActivity(intent);
                return;
            }

            if (ids[12] == id) {
                //去珀莱雅海洋透皙白莹亮乳
                Toast.makeText(v.getContext(), "去珀莱雅海洋透皙白莹亮乳", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 30);
                mContext.startActivity(intent);
                return;
            }
            if (ids[13] == id) {
                //去珀莱雅海洋活能密集精华液
                Toast.makeText(v.getContext(), "去珀莱雅海洋活能密集精华液", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 32);
                mContext.startActivity(intent);
                return;
            }
            if (ids[14] == id) {
                //去巴黎欧莱雅
                Toast.makeText(v.getContext(), "去巴黎欧莱雅", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 33);
                mContext.startActivity(intent);
                return;
            }
            if (ids[15] == id) {
                //去雅诗兰黛
                Toast.makeText(v.getContext(), "去雅诗兰黛", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 34);
                mContext.startActivity(intent);
                return;
            }
            if (ids[16] == id) {
                //去相宜本草
                Toast.makeText(v.getContext(), "去SKLL", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 35);
                mContext.startActivity(intent);
                return;
            }
            if (ids[17] == id) {
                //去SKLL
                Toast.makeText(v.getContext(), "去相宜本草", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, ProductActivity.class);
                intent.putExtra(CommonConstant.PRODUCT_ID_KEY, 36);
                mContext.startActivity(intent);
                return;
            }
        }
    };

}
